Astronouts

Libraries

tidyverse: data transformations and beautiful plots (Wickham et al. 2019).
hrbrthemes: this contains the ipsum theme, a very simple and elegant theme (Rudis 2020).
rmarkdown: all the structure of the report relies on this library (Allaire et al. 2020).
bookdown: allows the bibliography on the YAML header of this Rmarkdown doc (Xie 2016, 2020a).
epuRate: the elegant theme of the report (Holtz 2020).
knitr: all parts integration to render the output reporducible report (Xie 2014, 2015, 2020b).
citr: addin for easyly find citations in the .bib file and insert in the correct format (Aust 2019).
icon: inserting many different icons in markdown (O’Hara-Wild 2020).
gganimate: animating ggplot objects (Pedersen and Robinson 2020).


Getting TidyTuesday data

A very handy way to get data from the original TidyTuesday repo is using its own package functions: data = tidytuesdayR::tt_load(2020, week = 29) and then we need to store the element of this list as the actual dataset as astronauts = data$astronauts so the head looks like:


Sometimes head() function doesn’t say to much, so lets use the glimpse function of the {tidyduesdayR} package to take a good look into the data:

  Rows: 1,277
  Columns: 24
  $ id                       <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1…
  $ number                   <dbl> 1, 2, 3, 3, 4, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9,…
  $ nationwide_number        <dbl> 1, 2, 1, 1, 2, 2, 2, 4, 4, 3, 3, 3, 4, 4, 5,…
  $ name                     <chr> "Gagarin, Yuri", "Titov, Gherman", "Glenn, J…
  $ original_name            <chr> "ГАГАРИН Юрий Алексеевич", "ТИТОВ Герман Сте…
  $ sex                      <chr> "male", "male", "male", "male", "male", "mal…
  $ year_of_birth            <dbl> 1934, 1935, 1921, 1921, 1925, 1929, 1929, 19…
  $ nationality              <chr> "U.S.S.R/Russia", "U.S.S.R/Russia", "U.S.", …
  $ military_civilian        <chr> "military", "military", "military", "militar…
  $ selection                <chr> "TsPK-1", "TsPK-1", "NASA Astronaut Group 1"…
  $ year_of_selection        <dbl> 1960, 1960, 1959, 1959, 1959, 1960, 1960, 19…
  $ mission_number           <dbl> 1, 1, 1, 2, 1, 1, 2, 1, 2, 1, 2, 3, 1, 2, 1,…
  $ total_number_of_missions <dbl> 1, 1, 2, 2, 1, 2, 2, 2, 2, 3, 3, 3, 2, 2, 3,…
  $ occupation               <chr> "pilot", "pilot", "pilot", "PSP", "Pilot", "…
  $ year_of_mission          <dbl> 1961, 1961, 1962, 1998, 1962, 1962, 1970, 19…
  $ mission_title            <chr> "Vostok 1", "Vostok 2", "MA-6", "STS-95", "M…
  $ ascend_shuttle           <chr> "Vostok 1", "Vostok 2", "MA-6", "STS-95", "M…
  $ in_orbit                 <chr> "Vostok 2", "Vostok 2", "MA-6", "STS-95", "M…
  $ descend_shuttle          <chr> "Vostok 3", "Vostok 2", "MA-6", "STS-95", "M…
  $ hours_mission            <dbl> 1.77, 25.00, 5.00, 213.00, 5.00, 94.00, 424.…
  $ total_hrs_sum            <dbl> 1.77, 25.30, 218.00, 218.00, 5.00, 519.33, 5…
  $ field21                  <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
  $ eva_hrs_mission          <dbl> 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.…
  $ total_eva_hrs            <dbl> 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.…

Astronauts

countries <- astronauts %>% 
  select(year_of_mission, total_number_of_missions, nationality) %>%
  group_by(year_of_mission, nationality) %>% 
  mutate(nationality = as_factor(nationality), 
         year_of_mission = as.integer(year_of_mission), 
         ) %>% 
  summarise(
    count = sum(total_number_of_missions)
  ) %>% 
  ungroup() %>% 
  group_by(nationality) %>% 
  mutate(cumsum = cumsum(count)) %>% 
  ungroup() %>% 
  group_by(year_of_mission) %>% 
  mutate(ordering = rank(cumsum, ties.method = "max")) %>% 
  ungroup()


plot <- countries %>% 
  ggplot(aes(ordering, group = nationality))  +
  geom_tile(aes(y = cumsum/2, 
                height = cumsum,
                width = 0.7), 
            alpha = 0.6) +
  geom_text(aes(y = 0, label = nationality), hjust = 1.1) +
  coord_flip(clip = "off", expand = FALSE) +
  labs(
    title = "{frame_time}",
    subtitle = "",
    y = "Cumulative number of mission per year",
    x = "",
    caption = "Data: TidyTuesday week 29. Plot: @Gaspardelanoche",
    fill = ""
  ) +
  theme_ipsum() +
  theme(
    plot.title = element_text(size = 20, face = "bold"),
    plot.subtitle = element_text(size = 18),
    axis.title.x = element_text(size = 18),
    axis.title.y = element_text(size = 18),
    axis.text.y = element_blank(),
    axis.text.x = element_text(size = 14),
    axis.ticks.y = element_blank(),
    plot.caption = element_text(size = 12),
    plot.margin = margin(3, 3, 3, 3, "cm"),
  ) +
  transition_time(year_of_mission) +
  ease_aes('cubic-in') 

animate(plot, nframes = 500, fps = 10, width = 600, height = 600, end_pause = 25)
Fig 1. Comparison of the different coffee ratings for the different cups across countries and its associate distribution coffee cups.

Fig 1. Comparison of the different coffee ratings for the different cups across countries and its associate distribution coffee cups.





References

Allaire, JJ, Yihui Xie, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, Hadley Wickham, Joe Cheng, Winston Chang, and Richard Iannone. 2020. Rmarkdown: Dynamic Documents for R. https://CRAN.R-project.org/package=rmarkdown.

Aust, Frederik. 2019. Citr: RStudio Add-in to Insert Markdown Citations. https://CRAN.R-project.org/package=citr.

Holtz, Yan. 2020. EpuRate: A Clean Template for R Markdown Documents.

O’Hara-Wild, Mitchell. 2020. Icon: SVG Icons for R Documents and Apps. https://github.com/mitchelloharawild/icon.

Pedersen, Thomas Lin, and David Robinson. 2020. Gganimate: A Grammar of Animated Graphics. https://CRAN.R-project.org/package=gganimate.

Rudis, Bob. 2020. Hrbrthemes: Additional Themes, Theme Components and Utilities for ’Ggplot2’. https://CRAN.R-project.org/package=hrbrthemes.

Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain Fran??ois, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.

Xie, Yihui. 2014. “Knitr: A Comprehensive Tool for Reproducible Research in R.” In Implementing Reproducible Computational Research, edited by Victoria Stodden, Friedrich Leisch, and Roger D. Peng. Chapman; Hall/CRC. http://www.crcpress.com/product/isbn/9781466561595.

———. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. https://yihui.org/knitr/.

———. 2016. Bookdown: Authoring Books and Technical Documents with R Markdown. Boca Raton, Florida: Chapman; Hall/CRC. https://github.com/rstudio/bookdown.

———. 2020a. Bookdown: Authoring Books and Technical Documents with R Markdown. https://CRAN.R-project.org/package=bookdown.

———. 2020b. Knitr: A General-Purpose Package for Dynamic Report Generation in R. https://CRAN.R-project.org/package=knitr.

 




A work by Camilo Garcia